package algorithm.beforestart;

import java.util.HashSet;

public class singleNumber {


    public static int singleNumber(int[] nums) {
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                set.remove(nums[i]);
            } else {
                set.add(nums[i]);
            }
        }

        int result = 0;
        for (int key : set) {
            result = key;
        }
        return result;

    }

    public static int singleNumber_better(int[] nums) {

        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            result=result^nums[0];
        }
        System.out.println(result);
        return result;
    }

    public static void main(String[] args) {
        int[] nums ={4,1,2,1,2};
        singleNumber_better(nums);
    }
}
